package com.youlai.system.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.system.common.model.Option;
import com.youlai.system.common.result.PageResult;
import com.youlai.system.common.result.Result;
import com.youlai.system.model.form.PositionForm;
import com.youlai.system.model.query.PositionPageQuery;
import com.youlai.system.model.vo.PositionPageVO;
import com.youlai.system.plugin.dupsubmit.annotation.PreventDuplicateSubmit;
import com.youlai.system.service.SysPositionService;
import com.youlai.system.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 岗位控制器
 */
@Tag(name = "14.岗位接口")
@RestController
@RequestMapping("/api/v1/positions")
@RequiredArgsConstructor
public class SysPositionController {

    private final SysPositionService positionService;

    private final SysUserService userService;

    @Operation(summary = "岗位分页列表")
    @GetMapping("/page")
    public PageResult<PositionPageVO> getPositionPage(
            @ParameterObject PositionPageQuery queryParams
    ) {
        IPage<PositionPageVO> result = positionService.getPositionPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增岗位")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:position:add')")
    @PreventDuplicateSubmit
    public Result savePosition(
            @RequestBody @Valid PositionForm positionForm
    ) {
        boolean result = positionService.savePosition(positionForm);
        return Result.judge(result);
    }

    @Operation(summary = "岗位表单数据")
    @GetMapping("/{positionId}/form")
    public Result<PositionForm> getPositionForm(
            @Parameter(description = "岗位ID") @PathVariable Long positionId
    ) {
        PositionForm formData = positionService.getPositionForm(positionId);
        return Result.success(formData);
    }


    @Operation(summary = "修改岗位")
    @PutMapping(value = "/{positionId}")
    @PreAuthorize("@ss.hasPerm('sys:position:edit')")
    public Result updatePosition(
            @Parameter(description = "岗位ID") @PathVariable Long positionId,
            @RequestBody @Validated PositionForm positionForm) {
        boolean result = positionService.updatePosition(positionId, positionForm);
        return Result.judge(result);
    }

    @Operation(summary = "删除岗位")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:position:delete')")
    public Result deletePositions(
            @Parameter(description = "岗位ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "岗位删除数据为空");
        List<Long> idList = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        boolean positionHasUsed = userService.positionHasUsed(idList);
        Assert.isTrue(!positionHasUsed, "岗位使用中，不能被删除");
        boolean result = positionService.deletePositions(ids);
        return Result.judge(result);
    }

    @Operation(summary = "岗位下拉列表")
    @GetMapping("/options")
    public Result<List<Option>> listPositionOptions() {
        List<Option> list = positionService.listPositionOptions();
        return Result.success(list);
    }

}
